package com.xiangyang.pos.pay;


import com.xiangyang.pos.http.RequestParameters;
import com.xiangyang.pos.logger.Logger;
import com.xiangyang.pos.utils.CryptoUtil;

import java.security.NoSuchAlgorithmException;

/**
 * Created by Administrator on 2017/10/24 0024.
 */

public class WeixinPay {

    private String appid;//公众账号ID
    private String mch_id;//商户号
    private String nonce_str;//随机字符串
    private String sign;//签名
    private String body;//商品描述
    private String out_trade_no;//商户订单号
    private String total_fee;//订单金额
    private String spbill_create_ip;//终端IP
    private String auth_code;//授权码

    private String key;

    public WeixinPay() {
        appid="wxe890519a18b14b67";
        mch_id="1392661002";
        nonce_str=getNonce_str();
        body="泸州向阳网络科技有限公司";

        key="60E261F0a143b4ba49e80cd8383887E6";

    }

    //    支付需要上传的交易参数
    public RequestParameters getPayData(String auth_code) {
        RequestParameters parameters = new RequestParameters();
        parameters.put("appid", appid);
        parameters.put("mch_id", mch_id);
        parameters.put("body", "泸州向阳网络科技有限公司");
        parameters.put("nonce_str", nonce_str);
        parameters.put("out_trade_no", "1111111112");

        parameters.put("spbill_create_ip", "1111");
        parameters.put("total_fee", "1");
        parameters.put("auth_code", auth_code);

        parameters.put("sign", getSign(auth_code));

        return parameters;
    }

    public String getNonce_str() {

        String str="";
        for (int i = 0; i < 10; i++) {
            int a= (int) (Math.random()*10);
            str=str+a;
        }
        return str;
    }

    public String getSign(String auth_code) {
        String strA="appid=wxe890519a18b14b67" +
                "&auth_code="+auth_code+"" +
                "&body=泸州向阳网络科技有限公司" +
                "&mch_id=1392661002" +
                "&nonce_str="+nonce_str+"" +
                "&out_trade_no=1111111112" +
                "&spbill_create_ip=1111" +
                "&total_fee=1";

        String strSignTemp=strA+"&key=60E261F0a143b4ba49e80cd8383887E6" ;//注：key为商户平台设置的密钥key
        try {
            sign= CryptoUtil.Md5(strSignTemp); //注：MD5签名方式
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }

        Logger.e("xxxsign",sign);

        return sign;
    }


}
